package com.example.onion.hxchat.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.example.onion.hxchat.room.entity.User;
import com.example.onion.hxchat.test.CommentBean;
import com.example.superyi.myteam.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 自然就是现实评论子项布局用到的适配器，但是评论大佬做，先留着
 */
public class MyCommentAdapter extends RecyclerView.Adapter<MyCommentAdapter.ViewHolder> {


    List<User> mList = new ArrayList<>();
    Context mContext;
    private DataGetterListener mDataGetterListener;

    public MyCommentAdapter() {
    }

    public void setmList(List<User> mList) {
        this.mList = mList;
    }

    public MyCommentAdapter(List<User> mList, Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView commentName;

        TextView commentContent;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            commentName = itemView.findViewById(R.id.comment_name);
            commentContent = itemView.findViewById(R.id.comment_content);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final User commentBean = mList.get(position);
        holder.commentName.setText(commentBean.getUserName());
        holder.commentContent.setText(commentBean.getUserName());
        holder.commentName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = commentBean.getUserName();
                if (mDataGetterListener != null){
                    mDataGetterListener.getItemData(name);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
         return mList.size();

    }

    /**
     * 添加一条评论,刷新列表
     *
     * @param comment
     */
    public void addComment(User comment) {
        mList.add(comment);
        notifyDataSetChanged();
    }

    public interface DataGetterListener {//适配器内创建一个回调接口，获取每一行recyclerview的相关数据

        void getItemData(String name);//将本页面的获得的数据通过接口回调给调用方
    }

    public void setDataGetterListener(DataGetterListener dataGetterListener) {
        this.mDataGetterListener = dataGetterListener;
    }
}
